// pages/test/test.js
import {
  wushiyin
} from "../../utils/wushiyin.js";
import {
  random,
  selectYin,
  pingFindpian,
  pianFindping,
  title,
  titleType
} from "../../utils/_test.js"
Page({
  data: {
    radioItemsLeft: [{
        name: '清音',
        value: 'q',
        checked: 'true'
      },
      {
        name: '浊音',
        value: 'z',
        checked: 'false'
      },
      {
        name: '拗音',
        value: 'a',
        checked: 'false'
      }
    ],
    radioItemsRight: [{
        name: '5题',
        value: 5,
        checked: 'true'
      },
      {
        name: '10题',
        value: 10,
        checked: 'false'
      },
      {
        name: '15题',
        value: 15,
        checked: 'false'
      },
    ],
    hidden: false,
    next: true,
    correct: 0,
    wrong: 0,
    percent:0,
  },
  info: {
    type: 'q',
    num: 5
  },
  doneNum: 0,
  change: function(e) {
    let checked = e.currentTarget.dataset.value;
    let rIL = this.data.radioItemsLeft;
    rIL.forEach((v, i) => {
      if (v.value == checked) {
        v.checked = 'true'
        this.info.type = v.value
      } else {
        v.checked = 'false'
      }
    })
    this.setData({
      radioItemsLeft: rIL
    })
  },
  num_change: function(e) {
    let checked = e.currentTarget.dataset.value;
    let rIR = this.data.radioItemsRight;
    rIR.forEach((v, i) => {
      if (v.value == checked) {
        v.checked = 'true'
        this.info.num = v.value
      } else {
        v.checked = 'false'
      }
    })
    this.setData({
      radioItemsRight: rIR
    })
  },
  sure: function() {
    this.setData({
      hidden: true
    })
    this.createQuestion()
  },
  createQuestion: function() {
    let y = selectYin(this.info.type, wushiyin)
    let n, objs, a; //n 随机抽取的平片假名答案下标 objs 包含答案在内的 4 个选项  a  题目logo  
    let titleNum = random(0, title.length - 1)
    // 随机抽题
    if (titleNum <= 4) {
      n = random(0, y.ping.length - 1);
      a = y.ping[n];
      objs = pingFindpian(y, n)
    } else {
      n = random(0, y.pian.length - 1);
      a = y.pian[n]
      objs = pianFindping(y, n)
    }
    this.setData({
      title: title[titleNum],
      objs,
      a,
      next: false,
      curr: 4,
      titleType: titleType[titleNum % 4]
    })

  },
  play: function(e) {
    if (this.audio != null) {
      this.audio.destroy()
    }
    let text = encodeURI(e.currentTarget.dataset.text);
    let audio = wx.createInnerAudioContext();
    this.audio = audio;

    audio.src = `https://m.fanyi.qq.com/ttsfile?language=2&text=${text}&platform=H5`;
    audio.play()
  },
  choosed: function(e) {

    this.setData({
      next: true,
      curr: e.currentTarget.dataset.index,
      // real:e.currentTarget.dataset.real
    })
  },
  checkAndNext: function() {
    if (this.data.next) {
      let o, objs = this.data.objs,correct = this.data.correct,wrong = this.data.wrong//正确答案的下标
      objs.forEach( (v,i) => {
        if(v.real == true){
          o = i;
          return
        }
      })
      objs[o].finalAnswer = 'item_right'
      if(this.data.curr != o){
        let objs = this.data.objs, text = this.data.a.text, title = this.data.title, curr = this.data.curr,storage
        objs[curr].finalAnswer = 'item_wrong';
        wrong+=1;
        // 做错
        // console.log(this.data.objs,this.data.a.text,this.data.title,this.data.curr)
        storage = wx.getStorageSync("wrongCollection") || []
        storage.push({
          items:objs,
          title,
          curr,
          text
        })
        wx.setStorage({
          key: 'wrongCollection',
          data: storage,
        })
      }else{
        correct+=1;
        // 正确
      }
      this.doneNum+=1;
      this.setData({
        objs,
        wrong,
        correct,
        percent: (this.doneNum / this.info.num)*100
      })

      setTimeout(this.createQuestion, 800)
    }
    else {
      wx.showToast({
        title: 'A~还没选呢(*/ω＼*)',
        icon: 'none'
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})